import pygame
import sys
from dlgo.agent.naive import RandomBot
#from dlgo import goboard_slow
from dlgo import goboard
from dlgo import gotypes
from dlgo.utils import print_board, print_move
import time

window_width = 400
board_size = 9

def main():
    # render init
    cell_len = window_width / (board_size + 1)
    board_width = cell_len * (board_size - 1)
    piece_radius =  cell_len / 2.25
    color_white = (255, 255, 255)
    color_black = (0, 0, 0)
    pygame.init()
    screen = pygame.display.set_mode((window_width, window_width))
    
    #game = goboard_slow.GameState.new_game(board_size)
    game = goboard.GameState.new_game(board_size)
    bots = {
        gotypes.Player.black: RandomBot(),
        gotypes.Player.white: RandomBot(),
    }
    
    while not game.is_over():
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        
        time.sleep(0.3)
        
        #render board
        screen.fill((255, 200, 0))
        for row in range(1, board_size + 1):
            pos = cell_len * row
            pygame.draw.line(screen, color_black, (cell_len, pos), (cell_len + board_width, pos), 1)
            pygame.draw.line(screen, color_black, (pos, cell_len), (pos, cell_len + board_width), 1)
        print_board(game.board)
        bot_move = bots[game.next_player].select_move(game)
        #render pieces
        for row in range(board_size, 0, -1):
            for col in range(1, board_size + 1):
                pos = (col * cell_len, window_width - row * cell_len)
                stone = game.board.get(gotypes.Point(row=row, col=col))
                if stone == gotypes.Player.black:
                    pygame.draw.circle(screen, color_black, pos, piece_radius, 0)
                elif stone == gotypes.Player.white:
                    pygame.draw.circle(screen, color_white, pos, piece_radius, 0)
        pygame.display.flip()
        game = game.apply_move(bot_move)
    pygame.quit()

if __name__ == '__main__':
    main()